package wiki.douzi.helpyougrow.controller;

import wiki.douzi.helpyougrow.domain.FoodsCategory;
import wiki.douzi.helpyougrow.service.FoodsCategoryService;
import wiki.douzi.helpyougrow.enumeration.ResponseCodeEnum;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

/**
 * (FoodsCategory)表控制层
 *
 * @author Douzi
 * @since 2022-06-18 16:39:24
 */
@RestController
@RequestMapping("foodsCategory")
public class FoodsCategoryController {
    /**
     * 服务对象
     */
    @Resource
    private FoodsCategoryService foodsCategoryService;

    /**
     * 查询所有数据
     *
     * @return 实例对象集合
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> queryAll(
            @RequestParam(name = "page") String page,
            @RequestParam(name = "limit") String limit
    ) {
        Map<String, Object> resultMap = new HashMap<>();
        List<FoodsCategory> data = this.foodsCategoryService.queryAll();
        if (data != null) {
            resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
            resultMap.put("data", data);
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Map<String, Object>> queryById(@PathVariable("id") Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        FoodsCategory data = this.foodsCategoryService.queryById(id);
        if (data != null) {
            resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
            resultMap.put("data", data);
        } else {
            resultMap.put("msg", ResponseCodeEnum.SELECT_NULL.getMsg());
            resultMap.put("code", ResponseCodeEnum.SELECT_NULL.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 新增数据
     *
     * @param foodsCategory 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> add(@RequestBody FoodsCategory foodsCategory) {
        Map<String, Object> resultMap = new HashMap<>();
        FoodsCategory data = this.foodsCategoryService.insert(foodsCategory);
        if (data != null) {
            resultMap.put("msg", ResponseCodeEnum.INSERT_SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.INSERT_SUCCESS.getCode());
            resultMap.put("data", data);
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 编辑数据
     *
     * @param foodsCategory 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Map<String, Object>> edit(@RequestBody FoodsCategory foodsCategory) {
        Map<String, Object> resultMap = new HashMap<>();
        FoodsCategory data = this.foodsCategoryService.update(foodsCategory);
        if (data != null) {
            resultMap.put("msg", ResponseCodeEnum.MODIFY_SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.MODIFY_SUCCESS.getCode());
            resultMap.put("data", data);
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Map<String, Object>> deleteById(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        boolean data = this.foodsCategoryService.deleteById(id);
        if (data) {
            resultMap.put("msg", ResponseCodeEnum.DELETE_SUCCESS.getMsg());
            resultMap.put("code", ResponseCodeEnum.DELETE_SUCCESS.getCode());
        } else {
            resultMap.put("msg", ResponseCodeEnum.SYSTEM_ERROR.getMsg());
            resultMap.put("code", ResponseCodeEnum.SYSTEM_ERROR.getCode());
        }
        return new ResponseEntity<Map<String, Object>>(resultMap, HttpStatus.OK);
    }

}


